<?

$large_image_prefix = "o_"; 			// The prefix name to large image
$thumb_image_prefix = "x_";			// The prefix name to the thumb image
//$large_image_name = $large_image_prefix.$_SESSION['random_key'];     // New name of the large image (append the timestamp to the filename)
//$thumb_image_name = $thumb_image_prefix.$_SESSION['random_key'];     // New name of the thumbnail image (append the timestamp to the filename)							// Maximum file size in MB
//$max_width = "500";							// Max width allowed for the large image
//$thumb_width = "240";						// Width of thumbnail image
//$thumb_height = "240";						// Height of thumbnail image



function resizeImage($image,$width,$height,$scale) {
	list($imagewidth, $imageheight, $imageType) = getimagesize($image);
	$imageType = image_type_to_mime_type($imageType);
	$newImageWidth = ceil($width * $scale);
	$newImageHeight = ceil($height * $scale);
	$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
	switch($imageType) {
		case "image/gif":
			$source=imagecreatefromgif($image); 
			break;
	    case "image/pjpeg":
		case "image/jpeg":
		case "image/jpg":
			$source=imagecreatefromjpeg($image); 
			break;
	    case "image/png":
		case "image/x-png":
			$source=imagecreatefrompng($image); 
			break;
  	}
	imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
	
	switch($imageType) {
		case "image/gif":
	  		imagegif($newImage,$image); 
			break;
      	case "image/pjpeg":
		case "image/jpeg":
		case "image/jpg":
	  		imagejpeg($newImage,$image,200); 
			break;
		case "image/png":
		case "image/x-png":
			imagepng($newImage,$image);  
			break;
    }
	
	chmod($image, 0755);
	return $image;
}
//You do not need to alter these functions
function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){
	list($imagewidth, $imageheight, $imageType) = getimagesize($image);
	$imageType = image_type_to_mime_type($imageType);
	
	$newImageWidth = ceil($width * $scale);
	$newImageHeight = ceil($height * $scale);
	$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
	switch($imageType) {
		case "image/gif":
			$source=imagecreatefromgif($image); 
			break;
	    case "image/pjpeg":
		case "image/jpeg":
		case "image/jpg":
			$source=imagecreatefromjpeg($image); 
			break;
	    case "image/png":
		case "image/x-png":
			$source=imagecreatefrompng($image); 
			break;
  	}
	imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);
	switch($imageType) {
		case "image/gif":
	  		imagegif($newImage,$thumb_image_name); 
			break;
      	case "image/pjpeg":
		case "image/jpeg":
		case "image/jpg":
	  		imagejpeg($newImage,$thumb_image_name,200); 
			break;
		case "image/png":
		case "image/x-png":
			imagepng($newImage,$thumb_image_name);  
			break;
    }
	chmod($thumb_image_name, 0777);
	return $thumb_image_name;
}
//You do not need to alter these functions
function getHeight($image) {
	$size = getimagesize($image);
	$height = $size[1];
	return $height;
}
//You do not need to alter these functions
function getWidth($image) {
	$size = getimagesize($image);
	$width = $size[0];
	return $width;
}

 
$idUser = $objUser->isLoggedIn() ? $objUser->getIdUser() : 0;

if (isset($_POST["upload"]) && isset($_FILES['image']['name'])) { 
	
	$image = new Image('file', $_FILES['image'], array(
	'maxWidth' => 500, 
	'newExtension' => 'jpg', 
	'folder' => ABSPATH."images/avatars/", 
	'imageName' =>$idUser, 
	'deleteOnChange' => false), null); 
	$image->addArrSize(0, 0, 'o_'); 
	//$image->addArrSize(50, 50, 's_');
	//$image->addArrSize(650, 650, 'm_');
	
	$image->save();
}

if (isset($_POST["upload_thumbnail"]) && $large_photo_exists) {
	//Get the new coordinates to crop the image.
	$x1 = $_POST["x1"];
	$y1 = $_POST["y1"];
	$x2 = $_POST["x2"];
	$y2 = $_POST["y2"];
	$w = $_POST["w"];
	$h = $_POST["h"];

	$image = new Image('file', array(), array(
	'folder' => ABSPATH."images/avatars/", 
	'allowEmpty' => true,
	'imageName' => $idUser, 
	'imageType' => 'jpg', 
	'deleteOnChange' => false), null);
	$image->addArrCut($thumb_width, $thumb_height, 'x_', $x1, $y1, $w, $h); 
	$image->addArrCut(30, 30, 'chat_', $x1, $y1, $w, $h);
	
	$image->save();
	
	// сохраняем аватраку в таблице пользователя
	$objUser->saveUserAvatarToDB();
	
	header('Location: '.W_ABSPATH.'profile');

	$rnd = true;
}


if (isset($_GET['a']) && $_GET['a'] =="delete" && isset($_GET['t']) && strlen($_GET['t'])>0){
//get the file locations 
	$large_image_location = $upload_path.$large_image_prefix.$_GET['t'];
	$thumb_image_location = $upload_path.$thumb_image_prefix.$_GET['t'];
	if (file_exists($large_image_location)) {
		unlink($large_image_location);
	}
	if (file_exists($thumb_image_location)) {
		unlink($thumb_image_location);
	}
	if (file_exists('./images/avatars/i_'.$_COOKIE['id_us'].'.jpg')) {
		unlink('./images/avatars/i_'.$_COOKIE['id_us'].'.jpg');
	}
	header("location:".$_SERVER["PHP_SELF"]);
	exit(); 
}

?>